<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="100px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="分类" prop="categoryId">
        <category-query ref="categoryQuery" v-model="submitForm.categoryId" @choose="handleChangeCategory" />
      </el-form-item>
      <el-form-item label="属性key" prop="attributeKey">
        <el-input v-model="submitForm.attributeKey" placeholder="请输入属性key" />
      </el-form-item>
      <el-form-item label="属性名称" prop="name">
        <el-input v-model="submitForm.name" placeholder="请输入属性名称" />
      </el-form-item>
      <el-form-item label="选项类型" prop="optionType">
        <dict-select v-model="submitForm.optionType" dict-key="option_type" placeholder="请选择选项类型" />
      </el-form-item>
      <el-form-item v-if="submitForm.optionType!=='2'" label="选项" prop="optional">
        <el-input v-model="submitForm.optional" placeholder="请输入选项 多个用|分隔" />
      </el-form-item>
      <el-form-item v-if="submitForm.optionType!=='2'" label="可手动添加" prop="manuallyAdded">
        <dict-select v-model="submitForm.manuallyAdded" dict-key="true_or_false" placeholder="请选择是否可手动添加" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="submitForm.sort" :min="0" placeholder="请输入排序" style="width:100%" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveCategoryAttribute, updateCategoryAttributeById, selectCategoryAttributeById } from '@/api/erp/categoryAttribute'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        id: '',
        attributeKey: '',
        name: '',
        categoryId: '',
        optional: '',
        optionType: '',
        manuallyAdded: '',
        sort: ''
      },
      submitFormRules: {
        key: [{ required: true, message: '请输入属性key', trigger: 'change' }],
        attributeKey: [{ required: true, message: '请输入属性名称', trigger: 'change' }],
        categoryId: [{ required: true, message: '请选择分类', trigger: 'change' }],
        optional: [{ required: true, message: '请输入可选项', trigger: 'change' }],
        optionType: [{ required: true, message: '请选择选项类型', trigger: 'change' }],
        manuallyAdded: [{ required: true, message: '请选择是否支持手动添加', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增分类属性'
        return
      }
      this.dialogTitle = '修改分类属性'
      this.formLoading = true
      selectCategoryAttributeById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.$refs.categoryQuery.init(this.submitForm.categoryId)
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveCategoryAttribute
          if (this.submitForm.id) {
            request = updateCategoryAttributeById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleChangeCategory(categoryId) {
      this.submitForm.categoryId = categoryId
    }
  }
}
</script>

<style scoped>

</style>
